Accessibility

Creating the DataGrid

Now that you have a data source, use the following steps to create the DataGrid.

  1. Open the Server Behaviors tab of the Application panel.
  2. Select the plus button again, but this time select DataGrid as shown in the illustration below.

    Figure 9. Creating a DataGrid

    Figure 9. Creating a DataGrid

  3. Name your DataGrid dg_employees.
  4. Select the ds_employees DataSet from the popup list.
  5. Show just four Records instead of the default 10 records.
  6. Leave the rest of the attributes at their default values for now. The DataGrid dialog should appear as shown in the illustration below.
    Note: A change in the ASP.NET Framework version 1.1 caused a rendering issue in the Dreamweaver MX design view. If you use ASP.NET Framework version 1.1, you will see the ASP.NET syntax shield instead of the DataGrid in design view. To correct this, download and install the Dreamweaver MX extension fix for .NET framework version 1.1. For the extension fix and more information on this issue, see Dreamweaver compatibility issues with .NET Framework 1.1 (TechNote 18682).

    Figure 10. Specifying the DataGrid Attributes

    Full-size image

    Figure 10. Specifying the DataGrid Attributes

  7. Click OK to create the DataGrid.
  8. Save your work.

The dg_employees DataGrid you just created contains all of the columns that are in the DataSet you specified as the data source. The columns, by default, appear in the order that they are selected for the DataSet (see the SELECT statement in the DataSet tag in the above code listing).

Dreamweaver MX creates a design-time representation of the DataGrid in design view for you. Notice that the number of rows displayed is always 10 regardless of the number you specified when you created the DataGrid. Dreamweaver MX uses the placeholder string "abc" instead of the actual data.

Figure 11. Design-time representation of the DataGrid in design view

Full-size image

Figure 11. Design-time representation of the DataGrid in design view

To see your page in action, view the page in Live Data preview (View > Live Data), or press the F12 key to preview the page in your browser as shown in the illustration below.  

Figure 12. Previewing the DataGrid in the browser

Full-size image

Figure 12. Previewing the DataGrid in the browser

The file listEmployees-1.aspx included in the download zip file corresponds to this screen shot.

The column headers are taken from the names of the database columns. You'll learn how to change those as well as several other display attributes in my upcoming article on Customizing the Look of the DataGrid.

The code listing below shows the code generated by Dreamweaver MX for the DataGrid. Note that Dreamweaver automatically adds the form tag since it's necessary for the DataGrid. The code generated by Dreamweaver MX contains much more information than is minimally necessary to construct a DataGrid. It's a convenient launching point from which to customize the DataGrid.

<form runat="server">
	<asp:DataGrid id="dg_employees" 
		 runat="server" 
		 AllowSorting="False" 
		 AutoGenerateColumns="false" 
		 CellPadding="3" 
		 CellSpacing="0" 
		 ShowFooter="false" 
		 ShowHeader="true" 
		 DataSource="<%# ds_employees.DefaultView %>" 
		 PagerStyle-Mode="NextPrev" 
		 AllowPaging="true" 
		 AllowCustomPaging="true" 
		 PageSize="<%# ds_employees.PageSize %>" 
		 VirtualItemCount="<%# ds_employees.RecordCount %>" 
		 OnPageIndexChanged="ds_employees.OnDataGridPageIndexChanged" 
	 >     
	 <HeaderStyle HorizontalAlign="center" BackColor="#E8EBFD" ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif" Font-Bold="true" Font-Size="smaller" />
	 <ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial, Helvetica, sans-serif" Font-Size="smaller" />
	 <AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana, Arial, Helvetica, sans-serif" Font-Size="smaller" />
	 <FooterStyle HorizontalAlign="center" BackColor="#E8EBFD" ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif" Font-Bold="true" Font-Size="smaller" />
	 <PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica, sans-serif" Font-Size="smaller" />
	 <Columns>
	 <asp:BoundColumn DataField="EmployeeID" 
		 HeaderText="EmployeeID" 
		 ReadOnly="true" 
		 Visible="True"/>
	 <asp:BoundColumn DataField="LastName" 
		 HeaderText="LastName" 
		 ReadOnly="true" 
		 Visible="True"/>
	 <asp:BoundColumn DataField="FirstName" 
		 HeaderText="FirstName" 
		 ReadOnly="true" 
		 Visible="True"/>
	 <asp:BoundColumn DataField="HireDate" 
		 HeaderText="HireDate" 
		 ReadOnly="true" 
		 Visible="True"/>
	 <asp:BoundColumn DataField="Address" 
		 HeaderText="Address" 
		 ReadOnly="true" 
		 Visible="True"/>
	 <asp:BoundColumn DataField="City" 
		 HeaderText="City" 
		 ReadOnly="true" 
		 Visible="True"/>
	 <asp:BoundColumn DataField="Region" 
		 HeaderText="Region" 
		 ReadOnly="true" 
		 Visible="True"/>
	 <asp:BoundColumn DataField="PostalCode" 
		 HeaderText="PostalCode" 
		 ReadOnly="true" 
		 Visible="True"/>
	 <asp:BoundColumn DataField="Country" 
		 HeaderText="Country" 
		 ReadOnly="true" 
		 Visible="True"/>
	 <asp:BoundColumn DataField="HomePhone" 
		 HeaderText="HomePhone" 
		 ReadOnly="true" 
		 Visible="True"/>
	 </Columns>
	 </asp:DataGrid>
	 </form>

In Dreamweaver MX, the DataGrid and the DataSet used as the DataGrid's data source work closely together. For example, when you created the DataGrid and specified that it should show just 4 records at a time, Dreamweaver automatically added the PageSize attribute to the DataSet tag. The modified DataSet code is shown in code listing below.

<MM:DataSet 
		 id="ds_employees"
		 runat="Server"
		 IsStoredProcedure="false"
		 ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings["MM_CONNECTION_STRING_Northwind"]     %>'
		 DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings["MM_CONNECTION_DATABASETYPE_Northwind"]     %>'
		 CommandText='<%# "SELECT EmployeeID, LastName, FirstName, HireDate, Address, City, Region, PostalCode, Country, HomePhone FROM dbo.Employees ORDER BY LastName ASC" %>'
		 Debug="true" 
		  PageSize="4"
	></MM:DataSet>

The only difference is the addition of the PageSize attribute, which the PageSize attribute in the DataGrid uses:

PageSize="<%# ds_employees.PageSize %>"

You'll learn more about paging and paging styles in my upcoming article on Customizing the look of the DataGrid.

Now that you know how to create DataSets and how to create the default display for the DataGrid using the data in those datasets, I will show you some of the finer features of how to tune the DataGrid. In the following weeks, three more of my tutorials will be featured on the Macromedia DevNet Center. I will cover customizing the look of the DataGrid, making the DataGrid editable, and sorting on DataGrid columns. Check back in a couple of weeks for the next DataGrid article in this series.

 

Submit feedback on our tutorials, articles, and sample applications.